<?php
namespace app\system\controller;

use app\common\model\ShopGoodsModel;
use app\common\model\ShopGoodsSkuModel;
use app\common\model\ShopOrderModel;
use app\common\model\ShopPromotionGroupbuyCartModel;
use app\common\model\ShopPromotionGroupbuyModel;
use think\Controller;
use think\Request;

class ShopPromotionGroupbuyController extends BaseController
{

    protected $goodsId;
    private $goodsModel;
    private $promotionGroupbuyModel;
    private $promotionGroupbuyCartModel;
    private $shopOrderModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->goodsId                = $this->request->param('goods_id', 0);
        $this->goodsModel             = new ShopGoodsModel();
        $this->promotionGroupbuyModel = new ShopPromotionGroupbuyModel();
        $this->promotionGroupbuyCartModel=new ShopPromotionGroupbuyCartModel();
        $this->shopOrderModel=new ShopOrderModel();
    }

    /**
     * 预售商品
     */
    public function index()
    {
        $goodsName = $this->request->param('goods_name');
        $map       = [];
        $query     = [];
        if (!empty($goodsName)) {
            $map['goods_name']   = ['like', '%' . $goodsName . '%'];
            $query['goods_name'] = $goodsName;
        }
        $list = $this->promotionGroupbuyModel->alias('t1')
            ->join("shop_goods t2", "t1.goods_id=t2.goods_id")
            ->where($map)->paginate(12, false, ['query' => $query]);
        cookie('__GOODSURL__',get_current_url());
        $this->assign('list', $list);
        $this->assign('query', $query);
        return $this->fetch();
    }

    public function itemList()
    {
        $goodsId = $this->request->param('goods_id/d');
        $list=$this->promotionGroupbuyCartModel->where(['goods_id'=>$goodsId])->paginate();
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 删除预售商品
     * @return Controller
     */
    public function del()
    {
        $goodsId = $this->request->param('goods_id/d');
        $this->goodsModel->where('goods_id', $goodsId)->setField('promotion_type', 0);
        $this->promotionGroupbuyModel->delData($goodsId);
        $this->success('删除成功！', cookie('__GOODSURL__'));
    }

    public function toOrder()
    {
        $model=new ShopGoodsSkuModel();
        $goodsId=$this->request->param('goods_id',0);
        $skuId=$this->request->param('sku_id',0);
        $goodsInfo=$this->goodsModel->getInfo($goodsId);
        $list=$this->promotionGroupbuyCartModel->where(['goods_id'=>$goodsId,'margin_status'=>1,'is_order'=>0])->select();
        $skuList=$model->where('goods_id',$goodsId)->select();
        $skuIds=array_column($skuList->toArray(),'sku_id');
        $num=array_sum(array_column($list->toArray(),'num'));

        $price=$this->goodsModel->calcPrice($goodsInfo[$skuId]['goods_price'],$num);
        $cartIds=[];
        foreach ($list as $index => $value) {
            $siteCartList=[];
            $goods                           = format_goods_content($goodsInfo);
            $goods['price']                  = floatval($price); //商品单价
            $goods['goods_total']            = $value['num'] * $price; //商品合计价格
            $goods['minimum']            = $skuList[$value['sku_id']]['minimum']; //最小起订量
            $goods['goods_stock']            = $skuList[$value['sku_id']]['goods_stock']; //商品库存
            $siteCartList['item_list'][] = $goods;
            $siteCartList['num']         = count($siteCartList['item_list']); //店铺名称
            $siteCartList['site_name']         = get_site_name($value['site_id']); //店铺名称
            $siteCartList['sku_ids']           = implode(',', $skuIds); //sku_id串
            $siteCartList['total_num']         = $value['num']; //总数量
            $siteCartList['total_price']       = $price*$value['num']; //总价格
            $siteCartList['total_give_point']  = 0; //总赠送积分
            $siteCartList['total_goods_point'] = 0; //总消费积分
            $orderSn=$this->shopOrderModel->subOrder(0,
                $value['sku_id'],
                $value['member_uid'],
                $value['site_id'],$value['address_id'],0,1,'',0,0,'',$siteCartList
                );
            if ($orderSn){
                $cartIds[]=$value['cart_id'];
            }
        }
        $this->promotionGroupbuyCartModel->where('cart_id','in',$cartIds)->setField('is_order',1);
        $this->success('成功转化为订单');
    }
}
